using System;
using System.ComponentModel;
using System.Threading;

namespace DPLAB.IPAMS.IPAMSGui.Form
{
    public partial class frmWait : frmBase
    {
        #region Delegates

        public delegate void CloseFormCallback();

        #endregion

        /// <summary>
        /// Holds the current instance of the form, so the form will have only one instance
        /// maximum (we can't show more than one wait form)
        /// </summary>
        private static frmWait m_waitForm;

        #region Public Static Functions

        public static void ShowWaitScreen(string waitMsg)
        {
            m_waitForm = new frmWait();
            frmMain.GetInstance().Refresh();
            m_waitForm.lblMsg.Text = waitMsg;
            m_waitForm.prgProgressDisplay.Show();
            m_waitForm.TopMost = true;

            m_waitForm.wrkBackgrounWorker.RunWorkerAsync();
        }

        public static void CloseWaitScreen()
        {
            // if there is no wait form which is currently displayed, then there 
            //      is nothing to do
            if (m_waitForm == null)
                return;

            // some times the invoke fails because timing stuff, so try 2 times 
            //  to preform the invoke, and wait at least 3 seconds between one 
            //  failure to the next try
            int curr_try_number = 0;
            try
            {
                m_waitForm.Invoke(new CloseFormCallback(closeFormCallback));
            }
            catch (Exception)
            {
                curr_try_number += 1;
                if (curr_try_number > 2)
                    throw;

                Thread.Sleep(3000);
            }
        }

        #endregion

        #region GUI Callback Functions 

        /// <summary>
        /// This function is the callback function which closes the wait form. This
        /// function is being invoked from the GUI thread (and that is why it is a 
        /// callback)
        /// </summary>
        private static void closeFormCallback()
        {
            // close the wait form
            m_waitForm.Close();

            // set the instance to null (to force garbage collection of the form object)
            m_waitForm = null;
        }

        /// <summary>
        /// This function is the callback function which shows the wait form as a 
        /// dialog form. This function is being invoked from the background worker
        /// (and that is why it is a callback)
        /// </summary>
        private static void showWaitFormCallback()
        {
            m_waitForm.ShowDialog();
        }

        #endregion

        #region Form And Controls Events Code

        private frmWait()
        {
            InitializeComponent();
        }

        private void wrkBackgrounWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            showWaitFormCallback();
        }

        #endregion
    }
}